package com.evan.produce;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author huangzhongliang
 * @time 2023/06/06 19:53
 */
public class TicketLock {

    //3个人卖30张票 不出现超卖
    private int ticketNum = 30;

    //创建可重入锁
    private final ReentrantLock lock = new ReentrantLock();

    public void saleTicket() {
        //上锁
        lock.lock();
        try {
            if (ticketNum > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出时数量:" + (ticketNum--) + "剩下:" + ticketNum);
            }
        } finally {
            lock.unlock();
        }
    }
}
